package wf.source.hrm.rabbitmq.handler;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import wf.source.hrm.config.RabbitmqConfig;
import wf.source.hrm.domain.Sso;
import wf.source.hrm.util.SmsUtil;

/**
 * 消费者
 */
@Component
public class ReceiveHandler {
    @Autowired
    private JavaMailSender javaMailSender;

    //监听email队列消息
    @RabbitListener(queues = {RabbitmqConfig.QUEUE_INFORM_EMAIL})
    public void receive_email(String msg, Message message, Channel channel ){
        Sso sso = JSONObject.parseObject(msg, Sso.class);
        String email = sso.getEmail();

//        String email = JSONObject.parseObject(msg).getString("email");
//        if (StringUtils.isNotBlank(email)){
//            System.out.println("email receive:"+msg);
//
//        }
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        //发送人
        mailMessage.setFrom("2578024521@qq.com");
        //收件人
        mailMessage.setTo("2578024521@qq.com");
        //标题
        mailMessage.setSubject("你好，恭喜您注册成功");
        //内容
        mailMessage.setText("邮件测试");
        javaMailSender.send(mailMessage);
        System.out.println("发送邮件了");
        /*System.out.println("email_receive"+msg);
        String phone=msg;
        SmsUtil.sendMsg(phone,"尊敬的用户，您好！欢迎您的加入！");*/
    }
    //监听sms队列
    @RabbitListener(queues = {RabbitmqConfig.QUEUE_INFORM_SMS})
    public void receive_sms(String msg, Message message, Channel channel ){
        System.out.println("sms_receive"+msg);
//        String phone = JSONObject.parseObject(msg).getString("phone");
//        if (StringUtils.isNotBlank(phone)){
//            System.out.println("sms receive:"+msg);
//            SmsUtil.sendMsg(phone,"尊敬的用户您好！欢迎您注册本系统！");
//        }
        Sso sso = JSONObject.parseObject(msg, Sso.class);
        String phone = sso.getPhone();
        SmsUtil.sendMsg(phone, "恭喜您!注册成功,感谢您注册本系统！");
    }

  /*  public static void main(String[] args) {
        System.out.println(JSONObject.parseObject("{\"phone\":\"13330964751\",\"email\":\"2578024521@qq.com\"}"));
    }*/
}
